package com.xstudio.controllers;

import com.sun.javafx.scene.control.skin.VirtualFlow;
import com.xstudio.dao.*;
import com.xstudio.models.*;
import com.xstudio.mybatis.Pagination;
import com.xstudio.utilities.CommonUtility;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by kira on 16/2/28.
 */
@Controller
@RequestMapping(value = "/admin/roles")
public class RoleManageController {
    String prefix = "admin/roles/";

    @Resource
    SysRoleMapper sysRoleMapper;

    @Resource
    SysRoleMapperExtend sysRoleMapperExtend;

    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request, Model model) {
        Pagination pagination = CommonUtility.getPagination(request, model);
        HashMap<String, String> map = CommonUtility.getParameterMap(request);
        CommonUtility.renderGridData(request, model,
                sysRoleMapperExtend.all(pagination, map),
                pagination);

        return prefix + "index";
    }

    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, Model model) {
        return prefix + "add";
    }

    @RequestMapping(value = "/addEntity")
    @ResponseBody
    public String addEntity(HttpServletRequest request, Model model, SysRole sysRole) {
        sysRoleMapper.insertSelective(sysRole);
        return "";
    }

    @RequestMapping(value = "/edit")
    public String edit(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("entity", sysRoleMapper.selectByPrimaryKey(id));
        return prefix + "edit";
    }

    @RequestMapping(value = "/editEntity")
    @ResponseBody
    public String editEntity(HttpServletRequest request, Model model, SysRole sysRole) {
        sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        return "";
    }

    @RequestMapping(value = "/deleteEntity")
    @ResponseBody
    public String deleteEntity(HttpServletRequest request, Model model, Integer id) {
        sysRoleMapper.deleteByPrimaryKey(id);
        return "";
    }


    @Resource
    SysRoleUserMapper sysRoleUserMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @RequestMapping(value = "/configUser")
    public String configUser(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("list", sysUserMapper.selectByExample(null));

        SysRoleUserExample example = new SysRoleUserExample();
        example.createCriteria().andRoleidEqualTo(id);
        List<SysRoleUser> list = sysRoleUserMapper.selectByExample(example);

        List<Integer> userids = new ArrayList<>();
        for (SysRoleUser sysRoleUser : list) {
            userids.add(sysRoleUser.getUserid());
        }

        List<SysUser> userList = new VirtualFlow.ArrayLinkedList<>();
        if (userids.size() > 0) {
            SysUserExample userExample = new SysUserExample();
            userExample.createCriteria().andIdIn(userids);
            userList = sysUserMapper.selectByExample(userExample);
        }


        String useridStr = "";
        for (SysUser sysUser : userList) {
            useridStr += sysUser.getId() + ",";
        }
        if (StringUtils.isNotEmpty(useridStr)) {
            useridStr = useridStr.substring(0, useridStr.length() - 1);
        }
        model.addAttribute("userids", useridStr);
        model.addAttribute("entity", sysRoleMapper.selectByPrimaryKey(id));


        return prefix + "configUser";
    }

    @RequestMapping(value = "/configUserAction")
    @ResponseBody
    public String configUserAction(HttpServletRequest request, Model model,
                                   Integer id, String userids) {
        SysRoleUserExample example = new SysRoleUserExample();
        example.createCriteria().andRoleidEqualTo(id);
        sysRoleUserMapper.deleteByExample(example);

        if (StringUtils.isNotBlank(userids)) {
            String[] users = userids.split(",");
            for (String user : users) {
                SysRoleUser sysRoleUser = new SysRoleUser();
                sysRoleUser.setRoleid(id);
                sysRoleUser.setUserid(Integer.parseInt(user));
                sysRoleUserMapper.insertSelective(sysRoleUser);
            }
        }
        return "";
    }

    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;

    @Resource
    SysMenuMapper sysMenuMapper;

    @RequestMapping(value = "/configMenu")
    public String configMenu(HttpServletRequest request, Model model, Integer id) {
        model.addAttribute("list", sysMenuMapper.selectByExample(null));

        SysRoleMenuExample example = new SysRoleMenuExample();
        example.createCriteria().andRoleidEqualTo(id);
        List<SysRoleMenu> list = sysRoleMenuMapper.selectByExample(example);

        List<Integer> menuids = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu : list) {
            menuids.add(sysRoleMenu.getMenuid());
        }

        List<SysMenu> menus = new VirtualFlow.ArrayLinkedList<>();
        if (menuids.size() > 0) {
            SysMenuExample sysMenuExample = new SysMenuExample();
            sysMenuExample.createCriteria().andIdIn(menuids);
            menus = sysMenuMapper.selectByExample(sysMenuExample);
        }


        String menusidStr = "";
        for (SysMenu sysMenu : menus) {
            menusidStr += sysMenu.getId() + ",";
        }
        if (StringUtils.isNotEmpty(menusidStr)) {
            menusidStr = menusidStr.substring(0, menusidStr.length() - 1);
        }
        model.addAttribute("menuids", menusidStr);
        model.addAttribute("entity", sysRoleMapper.selectByPrimaryKey(id));


        return prefix + "configMenu";
    }

    @RequestMapping(value = "/configMenuAction")
    @ResponseBody
    public String configMenuAction(HttpServletRequest request, Model model,
                                   Integer id, String menuids) {
        SysRoleMenuExample example = new SysRoleMenuExample();
        example.createCriteria().andRoleidEqualTo(id);
        sysRoleMenuMapper.deleteByExample(example);

        if (StringUtils.isNotBlank(menuids)) {
            String[] menuidStr = menuids.split(",");
            for (String s : menuidStr) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuid(Integer.parseInt(s));
                sysRoleMenu.setRoleid(id);
                sysRoleMenuMapper.insertSelective(sysRoleMenu);
            }
        }
        return "";
    }
}
